package gen

import (
	"bytes"
	"fmt"
	"io/ioutil"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

var domainTemplate = `/** 
* go to java mybatis gen 
* @author hongyanyang1 
*/
package {{.Package}}.{{.BeanPkg}}{{moduleCrePkg .}};

import {{.Package}}.{{.BeanPkg}}.helper.BaseBean;

public class {{camel .Bean}} extends BaseBean<{{camel .Bean}}> {
	private static final long serialVersionUID = 1L;
	{{range .Cloums}}{{if (eqcolms .ColumnName "id" "remarks" "create_by" "create_date" "update_by" "update_date" "deleted")}}private {{fieldDbToJava .DataType}} {{tfname .ColumnName}};//{{.ColumnComment}}
	{{end}}{{end}}
	{{range .Cloums}}{{if (eqcolms .ColumnName "id" "remarks" "create_by" "create_date" "update_by" "update_date" "deleted")}}public {{fieldDbToJava .DataType}} {{genGet .ColumnName}}() {
		return {{tfname .ColumnName}};
	}
	
	public void {{genSet .ColumnName}}({{fieldDbToJava .DataType}} {{tfname .ColumnName}}) {
		this.{{tfname .ColumnName}} = {{tfname .ColumnName}};
	}
	{{end}}{{end}}
}
`

func GenDomain(genModules []Module) {
	fmt.Println("开始生成 domain...")
	for _, genModule := range genModules {
		if genModule.IsGenDomain {
			var project = genModule.SProject
			var cloums = genModule.Cloums
			var dir = PackageDir(genModule, "domain")
			MkdirAll(dir)
			for tableName, cloums := range cloums {
				buf := bytes.Buffer{}
				var tempData = NewTempData(tableName, cloums, project, genModule)
				templ := NewTmpl(domainTemplate)
				templ.Execute(&buf, tempData)
				err := ioutil.WriteFile(dir+"\\"+strings.Camel(tempData.Bean)+".java", buf.Bytes(), 0755)
				if err != nil {
					fmt.Println(err.Error())
				}
			}
		}
	}
}
